<template>
    <div>
        <el-container class="home-container">
            <el-header>
                <el-row>
                    <el-col :span="4">
                        <p class="system-name">后台管理</p>
                    </el-col>
                    <el-col :offset="12" :span="8" style="min-width: 150px">
                        <el-dropdown style="float:right; margin-right:20px 10px;">
                            <span class="el-dropdown-link" style="color: #fff; cursor: pointer">
                                {{user ? user.name : '访客'}} &nbsp;&nbsp;<i class="fa fa-caret-down fa-1x"></i>
                            </span>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item>修改密码</el-dropdown-item>
                                <el-dropdown-item @click.native="logout()">退出系统</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </el-col>
                </el-row>
            </el-header>
            <el-container style="overflow: auto">
                <el-aside>
                    <el-menu router :default-active="activePath" class="el-menu-vertical-demo" :collapse="isCollapse">
                        <div class="toggle-button" @click="isCollapse = !isCollapse">
                            <i v-if="isCollapse" class="el-icon-s-unfold"></i>
                            <i v-if="!isCollapse" class="el-icon-s-fold"></i>
                        </div>
                        <el-menu-item index="/index" @click="saveActiveNav('/index')">
                            <i class="el-icon-house"></i>
                            <span slot="title">首页</span>
                        </el-menu-item>
                        <el-submenu index="1">
                            <template slot="title">
                                <i class="el-icon-setting"></i>
                                <span slot="title">系统设置</span>
                            </template>
                            <el-menu-item>
                                <i class="el-icon-turn-off"></i>
                                <span slot="title">权限管理</span>
                            </el-menu-item>
                        </el-submenu>
                        <el-menu-item>
                            <i class="el-icon-user"></i>
                            <span slot="title">用户管理</span>
                        </el-menu-item>
                        <el-menu-item index="4">
                            <i class="el-icon-tickets"></i>
                            <span slot="title">订单管理</span>
                        </el-menu-item>
                        <el-menu-item index="/course/list" @click="saveActiveNav('/course/list')">
                            <i class="el-icon-notebook-1"></i>
                            <span slot="title"> 课程管理</span>
                        </el-menu-item>
                        <el-menu-item index="4">
                            <i class="el-icon-reading"></i>
                            <span slot="title">文章管理</span>
                        </el-menu-item>
                    </el-menu>
                </el-aside>
                <el-container>
                    <el-main>
                        <!-- 面包屑 -->
                        <Breadcrumb />
                        <!-- 内容 -->
                        <router-view></router-view>
                    </el-main>
                    <el-footer>Copyright @ 2022</el-footer>
                </el-container>
            </el-container>
        </el-container>
    </div>
</template>

<script>
import Breadcrumb from "../components/Breadcrumb.vue"
export default {
    components: { Breadcrumb },
    data() {
        return {
            isCollapse: false,
            activePath: "",
        }
    },
    created() {
        this.activePath = sessionStorage.getItem("activePath") ? sessionStorage.getItem("activePath") : "/index";
    },
    mounted() {
        this.user = JSON.parse(sessionStorage.user);
    },
    methods: {
        //保存链接的激活状态
        saveActiveNav(activePath) {
            sessionStorage.setItem("activePath", activePath);
            this.activePath = activePath;
        },
        logout() {
            this.$confirm("确定要退出系统吗?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
            .then(() => {
                //清除缓存
                sessionStorage.clear();
                this.$router.push("/login");
            })
            .catch(() => {
                return false;
            })
        }
    },
}
</script>

<style>
.home-container {
  position: absolute;
  height: 100%;
  top: 0px;
  left: 0px;
  width: 100%;
  background: #f2f3f5;
}

.el-header {
  background: #2661ef;
  padding: 0 10px;
  overflow: hidden;
}

.system-name {
  color: #fff;
  font-size: 18px;
}

.el-aside {
  background: white;
  width: auto !important;  /* 左侧宽度跟随 */
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-footer {
  color: #cccccc;
  text-align: center;
  line-height: 60px;
}

.el-footer:hover {
  color: #2661ef;
}

.toggle-button {
  background-color: #d9e0e7;
  font-size: 18px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  color: black;
}

/* :not(.el-menu--collapse) 收缩bug */
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu-item.is-active {
  color: #fff !important;
  font-size: 15px;
  font-weight: bold;
  background-color: #2661ef !important;
  border-radius: 2px;
  height: 50px;
  line-height: 50px;
  box-sizing: border-box;
  margin: 2px 5px 0px 2px;
}
</style>